package main

import (
	"fmt"
	"sync"
)

var once = sync.Once{}

// 1.保证类非公有化
type singleObject struct {
	Name string
}

// private
var instance *singleObject

func GetSingleInstance() *singleObject {
	if instance == nil {
		once.Do(func() {
			instance = &singleObject{
				Name: "123",
			}
		})
	}
	return instance
}

func main() {
	obj := GetSingleInstance()
	fmt.Println(obj)

	obj = nil
	obj = GetSingleInstance()
	fmt.Println(obj)
}
